package com.jiangjiang.eshop.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.order.convert.OrderItemConvert;
import com.jiangjiang.eshop.order.dao.OrderItemMapper;
import com.jiangjiang.eshop.order.domain.OrderItemEntity;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName OrderItemService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/24 11:39
 * @Version 1.0
 **/
@Service
public class OrderItemService extends ServiceImpl<OrderItemMapper, OrderItemEntity> {
    @Autowired
    private OrderItemConvert orderItemConvert;

    /**
     * 批量保存订单明细数据
     * @param orderInfoId 订单id
     * @param orderItems 订单明细数据
     */
    public void saveOrderItems(Long orderInfoId,List<OrderItemDTO> orderItems) {
        orderItems.forEach(item -> item.setOrderInfoId(orderInfoId));
        this.saveBatch( orderItemConvert.convertDTOList2Entities(orderItems));
    }

    /**
     * 根据订单编号查询订单明细数据
     * @param orderInfoId 订单编号
     * @return
     */
    public List<OrderItemDTO> queryByOrderInfoId(Long orderInfoId){
        LambdaQueryWrapper<OrderItemEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OrderItemEntity::getOrderInfoId, orderInfoId);
        return orderItemConvert.entities2DTOList(this.list(wrapper));
    }
}
